<template>
  <div>
    <div class="dashboard-container">
      <div class="app-container">
        <h2>员工</h2>
        <page-tools :show-before="true">
          <template #before> 共100条数据 </template>
          <template #after>
            <el-button size="small" type="primary"> execl导入</el-button>
            <el-button size="small" type="primary"> execl导出</el-button>
            <el-button size="small" type="primary" @click="showDialog = true"> 新增员工</el-button>
          </template>
        </page-tools>
        <el-card>
          <el-table :data="list" border>
            <el-table-column label="序号" sortable="" type="index" />
            <el-table-column label="姓名" sortable="" prop="username" />
            <el-table-column label="头像">
              <template slot-scope="{ row }">
                <img @click="showQr(row.staffPhoto)" style="width: 50px; height: 50px; border-radius: 50%" v-imageerror="require('@/assets/common/bigUserHeader.png')" :src="row.staffPhoto" alt="" />
              </template>
            </el-table-column>
            <el-table-column label="工号" sortable="" prop="workNumber" />
            <el-table-column label="聘用形式" :formatter="formatEmployment" sortable="" prop="formOfEmployment" />
            <el-table-column label="部门" sortable="" prop="departmentName" />
            <el-table-column label="入职时间" sortable="">
              <template slot-scope="{ row }">
                {{ row.timeOfEntry | formatDate }}
              </template>
            </el-table-column>
            <el-table-column label="账户状态" sortable="" prop="enableState">
              <template slot-scope="{ row }">
                <el-switch :value="row.enableState === 1" active-color="#13ce66" inactive-color="#ff4949" />
              </template>
            </el-table-column>
            <el-table-column label="操作" sortable="" fixed="right" width="280">
              <template slot-scope="{ row }">
                <el-button :disabled="checkPermission('POINT-USER-UPDATE')" type="text" size="small">查看</el-button>
                <el-button type="text" size="small">转正</el-button>
                <el-button type="text" size="small">调岗</el-button>
                <el-button type="text" size="small">离职</el-button>
                <el-button type="text" size="small" @click="editRole(row.id)">角色</el-button>
                <el-button type="text" size="small" @click="deleteEmployee(row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页组件 -->
          <el-row type="flex" justify="center" align="middle" style="height: 60px">
            <el-pagination layout="prev, pager, next" :page-size="page.size" :current-page="page.page" :total="page.total" @current-change="changePage" />
          </el-row>
        </el-card>
        <add-employee :show-dialog.sync="showDialog"></add-employee>
        <assign-role ref="assignRole" :user-id="userId" :show-role-dialog.sync="showRoleDialog"></assign-role>
        <el-dialog title="二维码" :visible.sync="showCodeDialog" @close="imgUrl = ''">
          <el-row type="flex" justify="center">
            <canvas ref="myCanvas" />
          </el-row>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import { getEmployeeList, delEmployee } from '@/api/employees'
import EmployeeEnum from '@/api/constant/employees'
import AddEmployee from './components/add-employee.vue'
import AssignRole from './components/assign-role.vue'
import QrCode from 'qrcode'
export default {
  name: 'Employees',

  data() {
    return {
      list: [], // 员工列表
      page: {
        page: 1,
        size: 10,
        total: 0
      },
      userId: null,
      showDialog: false,
      showCodeDialog: false,
      showRoleDialog: false // 角色列表弹框
    }
  },
  components: {
    AddEmployee,
    AssignRole
  },

  created() {
    this.getEmployeeList()
  },

  methods: {
    async getEmployeeList() {
      const res = await getEmployeeList(this.page)
      this.page.total = res.data.data.total
      this.list = res.data.data.rows
    },
    changePage(val) {
      this.page.page = val
      this.getEmployeeList()
    },
    formatEmployment(row, column, cellValue, index) {
      const obj = EmployeeEnum.hireType.find((item) => {
        return item.id === cellValue
      })
      return obj ? obj.value : '未知'
    },
    async deleteEmployee(id) {
      try {
        // 删除的弹窗
        await this.$confirm('你确定要删除给员工吗')
        // 调用删除员工的接口
        await delEmployee(id)
        // 重新获取员工列表
        this.getEmployeeList()
        this.$message.success('删除员工成功')
      } catch (error) {
        console.log(error)
      }
    },
    showQr(url) {
      this.showCodeDialog = true
      this.showRqcode(url)
    },
    showRqcode(url) {
      if (url) {
        this.$nextTick(() => {
          console.log('this.$refs.myCanvas:', this.$refs.myCanvas)
          QrCode.toCanvas(this.$refs.myCanvas, url, { width: 200, height: 200 })
        })
      } else {
        this.$message.warning('该用户还未上传头像')
      }
    },
    async editRole(id) {
      this.userId = id
      await this.$refs.assignRole.getUserDetailById(id)
      this.showRoleDialog = true
    }
  }
}
</script>

<style lang="scss" scoped></style>
